<template>
  <div class="login-container">
    <div class="login-card">
      <div class="login-header">
        <h2 class="login-title">工作流平台</h2>
        <p class="login-subtitle">让业务流程管理更简单</p>
      </div>

      <el-form
        ref="formRef"
        :model="loginForm"
        :rules="loginRules"
        class="login-form"
        size="large"
      >
        <el-form-item prop="username">
          <el-input
            v-model="loginForm.username"
            placeholder="用户名"
            :prefix-icon="User"
            clearable
          />
        </el-form-item>
        <el-form-item prop="password">
          <el-input
            v-model="loginForm.password"
            type="password"
            placeholder="密码"
            :prefix-icon="Lock"
            show-password
            clearable
            @keyup.enter="handleLogin"
          />
        </el-form-item>

        <el-form-item class="login-btn">
          <el-button
            :loading="loading"
            type="primary"
            class="login-submit"
            @click="handleLogin"
            size="large"
            round
          >
            登录
          </el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script setup lang="ts">
import { ref, reactive } from 'vue'
import { useRouter } from 'vue-router'
import type { FormInstance } from 'element-plus'
import { ElMessage } from 'element-plus'
import { User, Lock } from '@element-plus/icons-vue'
import { login } from '@/api/auth'
import { useUserStore } from '@/stores/user'

const router = useRouter()
const userStore = useUserStore()

const formRef = ref<FormInstance>()
const loading = ref(false)

const loginForm = reactive({
  username: 'baidu',
  password: 'baidu123',
})

const loginRules = {
  username: [{ required: true, message: '请输入用户名', trigger: 'blur' }],
  password: [{ required: true, message: '请输入密码', trigger: 'blur' }],
}

const handleLogin = async () => {
  if (!formRef.value) return
  await formRef.value.validate(async (valid) => {
    if (valid) {
      loading.value = true
      try {
        const res = await login(loginForm)
        // 保存token和用户信息
        userStore.setToken(res.data.token)
        userStore.setUsername(res.data.user.username)
        userStore.setTenantId(res.data.user.id)
        ElMessage.success('登录成功')
        // 登录成功后跳转到上次退出时的页面或首页
        const redirect = userStore.lastPath || '/'
        // 跳转后清除记录的路径
        userStore.setLastPath('')
        await router.push(redirect)
      } catch (error: any) {
        console.error('登录失败:', error)
        ElMessage.error(error.message || '登录失败')
      } finally {
        loading.value = false
      }
    }
  })
}
</script>

<style scoped>
.login-container {
  min-height: 100vh;
  display: flex;
  align-items: center;
  justify-content: center;
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  padding: 20px;
}

.login-card {
  width: 100%;
  max-width: 400px;
  padding: 40px 30px;
  background: white;
  border-radius: 10px;
  box-shadow: 0 15px 35px rgba(0, 0, 0, 0.1);
}

.login-header {
  text-align: center;
  margin-bottom: 30px;
}

.login-title {
  font-size: 28px;
  font-weight: 600;
  color: #333;
  margin: 0 0 10px 0;
}

.login-subtitle {
  font-size: 14px;
  color: #666;
  margin: 0;
}

.login-form :deep(.el-form-item) {
  margin-bottom: 20px;
}

.login-form :deep(.el-input__wrapper) {
  box-shadow: none;
  border: 1px solid #dcdfe6;
  padding: 8px 12px;
  border-radius: 4px;
  transition: all 0.3s ease;
  background-color: #fff;
}

.login-form :deep(.el-input__wrapper:hover) {
  border-color: #409eff;
}

.login-form :deep(.el-input__wrapper.is-focus) {
  border-color: #409eff;
  box-shadow: 0 0 0 2px rgba(64, 158, 255, 0.1);
}

.login-form :deep(.el-input__inner) {
  font-size: 14px;
  color: #333;
  background: transparent;
}

.login-form :deep(.el-input__prefix) {
  margin-right: 8px;
}

.login-form :deep(.el-input__icon) {
  font-size: 15px;
  color: #999;
}

.login-form :deep(.el-input__icon:hover) {
  color: #409eff;
}

.login-btn {
  margin-top: 35px;
  margin-bottom: 10px;
}

.login-submit {
  width: 100%;
  font-weight: 500;
  transition: all 0.3s;
  background: linear-gradient(to right, #409eff, #1890ff);
  border: none;
  font-size: 15px;
  padding: 10px;
  border-radius: 4px;
}

.login-submit:hover {
  box-shadow: 0 4px 12px rgba(64, 158, 255, 0.3);
}

/* 响应式布局 */
@media screen and (max-width: 768px) {
  .login-card {
    padding: 30px 20px;
  }

  .login-title {
    font-size: 24px;
  }
}
</style>
